﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sy2_3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            try
            {
                // 将输入的字符串转换为 double 类型
                double n = Convert.ToDouble(txtN.Text);
                int result;
                // 多分支 if - else if - else 结构判断
                if (n < 0)
                {
                    result
        = -1;
                }
                else if (n == 0)
                {
                    result
        = 0;
                }
                else
                {
                    result
        = 1;
                }
                // 将结果显示在结果文本框中
                txtResult
        .Text = result.ToString();
            }
            catch (FormatException)
            {
                MessageBox
        .Show("请输入有效的数字！", "输入错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtN
        .Clear();
                txtN
        .Focus();
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {

            // 清空两个文本框的内容
            txtN
        .Clear();
            txtResult
        .Clear();
            // 将焦点设置到输入文本框
            txtN
        .Focus();
        }
    }
}
